package org.scboy.rbac.service.imp;

import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacSystemDao;
import org.scboy.rbac.dao.RbacSystemGroupDao;
import org.scboy.rbac.service.RbacSystemGroupService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.scboy.utils.enums.coobi.OrderType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hutool.core.lang.UUID;

@Service
public class RbacSystemGroupServiceImp implements RbacSystemGroupService {

	@Autowired
	EntityFactory factory;
	
	@Override
	public Page page(Model params) {
		RbacSystemGroupDao dao =factory.getDao(RbacSystemGroupDao.class);
		params.orderBy("sortIndex");
		return dao.page(params);
	}

	@Override
	public boolean add(Model params) {
		RbacSystemGroupDao dao =factory.getDao(RbacSystemGroupDao.class);
		Long maxIndex=dao.max(new String [] {"sortIndex"}, Model.create()).getLong("sortIndex");
		if(maxIndex==null) {
			maxIndex=0L;
		}else {
			maxIndex+=1;
		}
		params.add("id", UUID.randomUUID().toString());
		params.add("sortIndex", maxIndex);
		int count = dao.insert(params);
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean update(Model params) {
		RbacSystemGroupDao dao =factory.getDao(RbacSystemGroupDao.class);
		int count = dao.update(params);
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public Model get(String id) {
		RbacSystemGroupDao dao =factory.getDao(RbacSystemGroupDao.class);
		return dao.get(Model.create().add("id", id));
	}

	@Override
	public boolean delete(String id) {
		RbacSystemGroupDao dao =factory.getDao(RbacSystemGroupDao.class);
		RbacSystemDao systemDao=factory.getDao(RbacSystemDao.class);
		long sysCount = systemDao.count(Model.create().add("groupId", id));
		if(sysCount>0) {
			return false;
		}
		int count = dao.delete(Model.create().add("id", id));
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean sort(String id, int type) {
		RbacSystemGroupDao dao =factory.getDao(RbacSystemGroupDao.class);
		Model data=dao.get(Model.create().add("id", id));
		Model lastModel=null;
		//降序
		if(type==0) {
			//查询下面一个系统
			lastModel=dao.get(Model.create().GT("sortIndex", data.getInt("sortIndex")).orderBy("sortIndex"));
		}else {
			//升序
			lastModel=dao.get(Model.create().LT("sortIndex", data.getInt("sortIndex")).orderBy("sortIndex",OrderType.DESC));
		}
		if(lastModel!=null) {
			dao.update(Model.create().add("sortIndex", lastModel.getInt("sortIndex")),Model.create().add("id", id));
			dao.update(Model.create().add("sortIndex", data.getInt("sortIndex")),Model.create().add("id", lastModel.getString("id")));
		}
		return true;
	}

	@Override
	public List<Model> list() {
		RbacSystemGroupDao dao =factory.getDao(RbacSystemGroupDao.class);
		return dao.select(Model.create().orderBy("sortIndex"));
	}

}
